package com.anquing.securitydemo.config;

import com.anquing.securitydemo.filter.TestingAuthenticationFilter;
import com.anquing.securitydemo.filter.phonecode.PhoneCodeAuthenticationConfigurer;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

import static org.springframework.security.config.Customizer.withDefaults;

/**
 * @author wangqq65
 * @since 2021/8/17 17:42
 */
@Configuration
public class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Resource
    private PhoneCodeAuthenticationConfigurer phoneCodeAuthenticationConfigurer;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.apply(phoneCodeAuthenticationConfigurer)
                .and()
                .addFilterAfter(new TestingAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .authorizeRequests(authorize -> authorize
                        .antMatchers("/loginByPhone").permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin(withDefaults());
    }
}
